<?php
namespace app\admin\model;
use think\Model;
use think\Db;
use app\admin\model\BusChannelSendConfig as ChannelSendConfigModel;

use app\admin\model\Channel as ChannelModel;
//通道组
use app\admin\model\BusPlatGroupConfig as PlatGroupModel;
/**
 * 基础分省模板
 * @package app\admin\model
 */
class MainTemplate extends Model
{


    public static function getCorrespondAll($request=[])
    {
        $data = self::alias('cor')
            ->field('cor.*,0+CAST(cor.price AS char) as price,cha.company_name,pla.platfrom,pla.plat_group,ope.desc,cous.correspond_count')
            ->join('bus_channel cha','cha.channel_id = cor.cid','LEFT')
            ->join('bus_platfrom pla','pla.plat_id = cor.plat_id','LEFT')
            ->join('main_operator ope','ope.code = cor.isp_host','LEFT')
            ->join('(select count(id) correspond_count,correspond_id from bus_plat_group_config group by correspond_id) cous','cor.id = cous.correspond_id','LEFT');

        // if(!empty($request['sign'])) {

        //     $data = $data->where('sign.sign', 'like' , '%' . $request['sign'] . '%' );
        // }

        if(!empty($request['ci'])) {

            $data = $data->where('cor.cid',$request['ci']);
        }

        if(!empty($request['pi'])) {

            $data = $data->where('cor.plat_id',$request['pi']);
        }

        //只查询出对应角色的
        switch (session('user_type')) {
            case 'admin':
                // $data = $data->where('cha.level','in','1,4');
                break;
            case 'channel':
                $channelArr = ChannelModel::where('p_id',session('relate_id'))->column('channel_id');
                $data = $data->where('cha.channel_id','in',$channelArr);
                break;
            default:
                return [];
                break;
        }

        $data = $data->order('cor.cid')
    		->paginate($request['limit'], false, ['query' => $request])
            ->each(function($item, $key) {

                // $item->neirong = $item->notes;
                // $item->price = 0+CAST(bps.original_price AS char) as original_price
                // if($item->plat_group == 'GROUP')
                // {
                    if(empty($item->correspond_count)){
                        $item->correspond_count = 0;
                    }
                    $item->correspond_count = '<a href="javascript:;" style="color:#01499C" onclick="onOpenRoute('.$item->id.','.$item->plat_id.')">配置('.$item->correspond_count.')</a>';
                // }else{
                //     $item->correspond_count = '<a href="javascript:;" style="color:#ccc" >配置(0)</a>';
                // }

                switch ($item->onoff) {
                    case '0':
                        $item->on = '<span class="layui-badge status-jy">已禁用</span>';
                        break;
                    case '1':
                        $item->on = '<span class="layui-badge layui-bg-green status-qy">已启用</span>';
                        break;

                    default:
                        # code...
                        break;
                }
                switch ($item->isp_host) {
                    case '10010':
                        $item->isp_host = '联通';
                        break;
                    case '10086':
                        $item->isp_host = '移动';
                        break;
                    case '10000':
                        $item->isp_host = '电信';
                        break;
                    case '90001':
                        $item->isp_host = '三网';
                        break;
                    default:
                        # code...
                        break;
                }
            });

    	return $data;
    }


    public static function add($data)
    {
       return self::create($data);
    }
}